﻿$(function () {
    $.kish.CommonSearch = {};
    $.extend($.kish.CommonSearch, {

        //********************************//
        WithTotal: null,
        keyWord: null,
        //set basid details
        initializing: function () {
            $.kish.CommonSearch.keyWord = $.Jahanutility.getParameterByName("keyWord");
            if ($.kish.CommonSearch.keyWord != '') {
                $.kish.CommonSearch.CommonSearchCar(0, 20, true);
                $.kish.CommonSearch.CommonSearchCarStore(0, 20, true);
                $.kish.CommonSearch.CommonSearchServiceCenter(0, 20, true);
            };

            $.Jahanutility.togglePanels($("#CarSearchHeader"), $(".CarSearchHeader"));
            $.Jahanutility.togglePanels($("#carstoreSearchHeader"), $(".carstoreSearchHeader"));
            $.Jahanutility.togglePanels($("#serivceSearchHeader"), $(".serivceSearchHeader"));
        },

        //**********************************************************
        CommonSearchCar: function (start, end, withtotal) {
            var SortByStartDate = null;
            if ($('.SortByStartDateDrp').val() != 0)
                SortByStartDate = $('.SortByStartDateDrp').val();
            var SortByStartDateVal = false;
            if (SortByStartDate != null) {
                SortByStartDateVal = true;
            }
            var isAscend = $('.IsAscend').val();

            $('#submit').attr('disabled', 'disabled');
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/SearchAllCar',
                'data': JSON.stringify({ txt: $.kish.CommonSearch.keyWord, Start: start, End: end, withTotal: withtotal, SortByStartDate: SortByStartDateVal, IsAscend: isAscend }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (data) {

                    if (data['d'] == null || data['d'].Data.length == 0) {
                        //                        $.kish.message.getErrorMessage("15");
                    }
                    else {

                        //*************Fill top cars announcement *************                        
                        $('#carResult').html('');
                        for (var i = 0; i < data['d'].Data.length; i++) {
                            var price = $.trim(data['d'].Data[i].Price);
                            var path = '../Images/carNoPhoto.gif';
                            if (data['d'].Data[i].CarImages[0] != null) {
                                var relatedPath = (data['d'].Data[i].CarImages[0].Path);
                                path = '../' + relatedPath.replace('~', '..');
                            }
                            var x = '<div class="three columns isotope-item ">' +
                                        '<a class="portfolio-item isotope " href="CarDetails.aspx?ID=' + data['d'].Data[i].ID + '">' +
                                            '<figure>' +
                                            '<div class="crop">' +
                                                '<img alt="" src="' + path + '">' +
                                            '</div>' +
						                        '<figcaption class="item-description">' +
							                        '<h5>' + data['d'].Data[i].Model.Code + ' ' + data['d'].Data[i].Model.Brand.Code + '</h5>' +
                            //							                        '<span>' + $.trim(data['d'][i].Color.Code) + '</span>' + ' ' +
                            //                                                    '<span>' + $.trim(data['d'][i].ProductYear) + '</span>' +
                                                  '<span >تومان </span>' + '<span>' + price + '</span>' +
                                                 '</figcaption>' +
					                       '</figure>' +
                                        '</a>' +
                                    '</div>';
                            $('#carResult').append(x);
                        };
                    };
                    if (withtotal && data['d'].TotalCount && data['d'].TotalCount > 0 && data['d'].TotalCount != null) {
                        $.kish.CommonSearch.WithTotal = data['d'].TotalCount;
                        $.kish.CommonSearch.SetPaging('.PagingDivCar', $.kish.CommonSearch.CommonSearchCar);
                    }
                },
                'error': function (error) {

                }
            });
        },
        CommonSearchCarStore: function (start, end, withtotal) {
            var SortByStartDate = null;
            if ($('.SortByStartDateDrp').val() != 0)
                SortByStartDate = $('.SortByStartDateDrp').val();
            var SortByStartDateVal = false;
            if (SortByStartDate != null) {
                SortByStartDateVal = true;
            }
            var isAscend = $('.IsAscend').val();

            $('#submit').attr('disabled', 'disabled');
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/SearchAllCarStore',
                'data': JSON.stringify({ txt: $.kish.CommonSearch.keyWord, Start: start, End: end, withTotal: withtotal, SortByStartDate: SortByStartDateVal, IsAscend: isAscend }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (data) {

                    if (data['d'] == null || data['d'].Data.length == 0) {
                        //                        $.kish.message.getErrorMessage("15");
                    }
                    else {

                        $('#carstoreResult').html('');
                        for (var i = 0; i < data['d'].Data.length; i++) {
                            var tel = $.trim(data['d'].Data[i].Telephone);
                            var path = '../Images/carstoreNoPhoto.gif';
                            if (data['d'].Data[i].File != null) {
                                var relatedPath = (data['d'].Data[i].File.Path);
                                path = '../' + relatedPath.replace('~', '..');
                            }
                            var x = '<div class="three columns isotope-item ">' +
                                        '<a class="portfolio-item isotope " href="CarDetails.aspx?ID=' + data['d'].Data[i].ID + '">' +
                                            '<figure>' +
                                            '<div class="crop">' +
                                                '<img alt="" src="' + path + '">' +
                                            '</div>' +
						                        '<figcaption class="item-description">' +
							                        '<h5>' + data['d'].Data[i].Name + '</h5>' +
                                                (tel.length > 0 ? '<span > تلفن </span>' + '<span>' + tel + '</span>' : '') +
                                                 '</figcaption>' +
					                       '</figure>' +
                                        '</a>' +
                                    '</div>';
                            $('#carstoreResult').append(x);
                        };
                        if (withtotal && data['d'].TotalCount && data['d'].TotalCount > 0 && data['d'].TotalCount != null) {
                            $.kish.CommonSearch.WithTotal = data['d'].TotalCount;
                            $.kish.CommonSearch.SetPaging('.PagingDivcarstore', $.kish.CommonSearch.CommonSearchCarStore);
                        }
                    };
                },
                'error': function (error) {

                }
            });
        },
        CommonSearchServiceCenter: function (start, end, withtotal) {
            var SortByStartDate = null;
            if ($('.SortByStartDateDrp').val() != 0)
                SortByStartDate = $('.SortByStartDateDrp').val();
            var SortByStartDateVal = false;
            if (SortByStartDate != null) {
                SortByStartDateVal = true;
            }
            var isAscend = $('.IsAscend').val();

            $('#submit').attr('disabled', 'disabled');
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/SearchAllServiceCenter',
                'data': JSON.stringify({ txt: $.kish.CommonSearch.keyWord, Start: start, End: end, withTotal: withtotal, SortByStartDate: SortByStartDateVal, IsAscend: isAscend }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (data) {

                    if (data['d'] == null || data['d'].Data.length == 0) {
                        //                        $.kish.message.getErrorMessage("15");
                    }
                    else {

                        //*************Fill top  services announcement *************                        
                        $('#serviceResult').html('');
                        for (var i = 0; i < data['d'].Data.length; i++) {
                            var tel = $.trim(data['d'].Data[i].Telephone);

                            var path = '../Images/serviceNoPhoto.gif';
                            if (data['d'].Data[i].File != null) {
                                var relatedPath = (data['d'].Data[i].File.Path);
                                path = '../' + relatedPath.replace('~', '..');
                            }
                            var x = '<div class="three columns isotope-item ">' +
                                        '<a class="portfolio-item isotope " href="CarDetails.aspx?ID=' + data['d'].Data[i].ID + '">' +
                                            '<figure>' +
                                            '<div class="crop">' +
                                                '<img alt="" src="' + path + '">' +
                                            '</div>' +
						                        '<figcaption class="item-description">' +
							                        '<h5>' + data['d'].Data[i].Name + '</h5>' +
                                                     (tel.length > 0 ? '<span > تلفن </span>' + '<span>' + tel + '</span>' : '') +
                                                '</figcaption>' +
					                       '</figure>' +
                                        '</a>' +
                                    '</div>';
                            $('#serviceResult').append(x);
                        };
                        if (withtotal && data['d'].TotalCount && data['d'].TotalCount > 0 && data['d'].TotalCount != null) {
                            $.kish.CommonSearch.WithTotal = data['d'].TotalCount;
                            $.kish.CommonSearch.SetPaging('.PagingDivserivce',$.kish.CommonSearch.CommonSearchServiceCenter);
                        }
                    };
                },
                'error': function (error) {

                }
            });
        },
        SetPaging: function (PaginDivClass,PagingFunction) {
            var withTotal = $.kish.CommonSearch.WithTotal;
            var PageCount = Math.ceil(withTotal / 20);
            var paging = '';
            for (var i = 0; i < PageCount; i++) {
                var PageItem = '<span class="pagingItem">' + (i + 1).toString() + '</span>';
                paging = paging + PageItem;
            }
            $(PaginDivClass).text('');
            $(PaginDivClass).append(paging);
            $(PaginDivClass).append('<span class="totalCount"> کل موارد یافت شده : ' + withTotal + '</span>');
            $('.pagingItem', PaginDivClass).click(function () {
                var pageIndex = $(this).text();
                PagingFunction((pageIndex - 1) * 20, pageIndex * 20, false);
            });
        }

    });
    $.kish.CommonSearch.initializing();
});



